package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData() {
        //获取今天的日期
        LocalDate today = LocalDate.now();
        //查询今日新增的用户量
        Integer newUsers = userMapper.countByCreateTime(today);
        //查询今日营业额
        Double turnover = ordersMapper.todayTurnover(today,Orders.COMPLETED);
        //查询今日下单人数
        Integer users = ordersMapper.countUsers(today,Orders.COMPLETED);
        BigDecimal bd1 = new BigDecimal(turnover + "");
        BigDecimal bd2 = new BigDecimal(users + "");
        //计算平均客单价
        double unitPrice = 0.0;
        if (users != 0){
            unitPrice = bd1.divide(bd2,2, RoundingMode.HALF_UP).doubleValue();
        }

        //查询今日完成的订单数
        Integer validOrderCount = ordersMapper.countByStatusToday(Orders.COMPLETED,today);
        //查询今日的所有订单数
        Integer count = ordersMapper.countByStatusToday(null,today);
        BigDecimal bd3 = new BigDecimal(validOrderCount + "");
        BigDecimal bd4 = new BigDecimal(count + "");
        //计算订单完成率
        double orderCompletionRate = 1.0;
        if (count != 0){
            orderCompletionRate = bd3.divide(bd4,2, RoundingMode.HALF_UP).doubleValue();
        }
        return new BusinessDataVO(turnover,validOrderCount,orderCompletionRate,unitPrice,newUsers);
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //查询停售套餐数量
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);
        //查询启售套餐数量
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        return new SetmealOverViewVO(sold,discontinued);
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        //查询停售菜品数量
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        //查询启售菜品数量
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        return new DishOverViewVO(sold,discontinued);
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate today = LocalDate.now();
        //查询所有订单数
        Integer allOrders = ordersMapper.countByStatusToday(null,today);
        //查询已取消数量
        Integer cancelledOrders = ordersMapper.countByStatusToday(Orders.CANCELLED,today);
        //查询已完成数量
        Integer completedOrders = ordersMapper.countByStatusToday(Orders.COMPLETED,today);
        //查询待派送数量
        Integer deliveredOrders = ordersMapper.countByStatusToday(Orders.CONFIRMED,today);
        //查询待接单数量
        Integer waitingOrders = ordersMapper.countByStatusToday(Orders.TO_BE_CONFIRMED,today);
        return new OrderOverViewVO(waitingOrders,deliveredOrders,completedOrders,cancelledOrders,allOrders);
    }
}
